local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local EventList = ServerOHOH.load.EventList();
local Tool=require(script.Parent.tool)
local gCfg=Tool.GetConfg()

local ItemTool=_G.CommonOHOH.load.ItemTool();
local function_list = ServerOHOH.function_list;
function M.new(self)
	return M(self);
end;
function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.player=self.parent
	self.ItemCtrl=self.parent:GetComponent(ServerOHOH.load.Server_ItemCtrl());

	self.dataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent());
    self.data = self.dataBase:GetData("activity_1");
	self.needTime=gCfg.OnlineTime;
	self:TaskDo();

	self.player.RobuxBuyDoneEvent:Connect(function(gid,gtype)
		if gtype=="skip_active_1" then
			self:AddEggCount()
			self:AddProcess(function()
				self:OpenOpEgg()
			end)
		end
	end)

end;
function  M.TaskDo(self)
	if  self.task then
		return;
	end
	self.task=_G.CommonOHOH.load.OnLineTask().new(self.parent,self.needTime,"activity_1");
	self.taskConnect=self.task.CompleteEvent:Connect(function()
		if self.taskConnect then
			self.taskConnect:Disconnect();
			self.taskConnect=nil;
		end
		self:AddEggCount();
		self.task=nil;
		self:TaskDo();
	end)

end
function M.AddEggCount(self)
	self.data.count=self.data.count+1;
	self:SubmitOpEggData();
end;

function M.OpenOpEgg(self)
	if self.data.count<=0 then
		return true;
	end
	local item_id="egg_"..gCfg.EggId;
	self.data.count=self.data.count-1;
	self.ItemCtrl:AddAnyThing(item_id,1);
	self:SubmitOpEggData();
	return true;
end;
function M.SubmitOpEggData(self)
	self.dataBase:Execut(function()
	end);
	self.parent:SynchDataToClient("activity_1");
end
function M.dtor(self)
	if self.taskConnect  then
		self.taskConnect:Disconnect();
		self.taskConnect=nil;
	end
	if self.task then
		self.task:dtor();
    end
	M.super.dtor(self)
end;

M:RegistFunction(function_list.open_opegg,M.OpenOpEgg)
return M;